export default {
  state: {
    // 自定义节点模板
    templateFile: undefined,

    // 模板文件变化
    templateFileChange: 0,

    // 已注册的节点
    registerNodes: [],

    // 已注册的节点名称
    registerNodeNames: ['rect', 'circle', 'ellipse', 'path', 'polygon', 'polyline', 'cylinder', 'image', 'text', 'text-block']
  },

  getters: {
    getRegisterNode: (state) => (shapeName) => {
      const index = state.registerNodes.findIndex(value => {
        return value.shape === shapeName
      })
      return state.registerNodes[index]
    }
  },

  mutations: {
    setTemplateFile(state, file) {
      state.templateFile = file
    },

    setTemplateFileChange(state) {
      state.templateFileChange++
      if (state.templateFileChange > 10000) {
        state.templateFileChange = 0
      }
    },

    clearRegisterNodes(state) {
      for (const node of state.registerNodes) {
        const index = state.registerNodeNames.findIndex(value => {
          return value === node.shape
        })
        if (index !== -1) {
          state.registerNodeNames.splice(index, 1)
        }
      }
      state.registerNodes = []
    },

    appendRegisterNode(state, nodeConfig) {
      state.registerNodeNames.push(nodeConfig.shapeName)
      state.registerNodes.push(nodeConfig.config)
    }
  }
}
